﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace Ams.Mvc.Bootstrap
{
    public static class BootstrapLabelExtensions
    {
        public static MvcHtmlString LabelFor<TModel, TValue>(this BootstrapHelper<TModel> helper, Expression<Func<TModel, TValue>> expression)
        {
            return LabelFor(helper, expression, labelText: null);
        }

        public static MvcHtmlString LabelFor<TModel, TValue>(this BootstrapHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, string labelText)
        {
            return LabelFor(helper, expression, labelText, htmlAttributes: null);
        }

        public static MvcHtmlString LabelFor<TModel, TValue>(this BootstrapHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
        {
            return LabelFor(helper, expression, null, htmlAttributes);
        }

        public static MvcHtmlString LabelFor<TModel, TValue>(this BootstrapHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes)
        {
            return LabelFor(helper, expression, null, htmlAttributes);
        }

        public static MvcHtmlString LabelFor<TModel, TValue>(this BootstrapHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, string labelText, object htmlAttributes)
        {
            return LabelFor(helper, expression, labelText, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }

        public static MvcHtmlString LabelFor<TModel, TValue>(this BootstrapHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, string labelText, IDictionary<string, object> htmlAttributes)
        {
            htmlAttributes = htmlAttributes ?? new Dictionary<string, object>();

            if (helper.Form != null)
            {
                if (helper.Form.FormType == Form.Inline)
                {
                    HtmlMod.AppendToAttributeList(ref htmlAttributes, "class", "sr-only");
                }
                else if (helper.Form.FormType == Form.Horizontal)
                {
                    HtmlMod.AppendToAttributeList(ref htmlAttributes, "class", "control-label");
                    HtmlMod.AppendToAttributeList(ref htmlAttributes, "class", helper.Form.LabelClasses);
                }
            }

            var html = LabelExtensions.LabelFor(helper.Html, expression, htmlAttributes);
            return html;
        }
    }
}
